package com.yanqu.road.server.pb;

import com.yanqu.road.entity.kowloon.KowloonBuildingData;
import com.yanqu.road.entity.kowloon.KowloonNpcAttributeData;
import com.yanqu.road.entity.kowloon.KowloonHeroData;
import com.yanqu.road.entity.kowloon.KowloonUserData;
import com.yanqu.road.entity.kowloon.battle.KowloonBattleData;
import com.yanqu.road.entity.kowloon.battle.KowloonBattleEnemyData;
import com.yanqu.road.entity.kowloon.battle.KowloonBattleHeroData;
import com.yanqu.road.entity.kowloon.common.KowloonBuffParam;
import com.yanqu.road.entity.kowloon.common.KowloonRange;
import com.yanqu.road.entity.kowloon.common.Vec2;
import com.yanqu.road.entity.kowloon.sanxiao.*;
import com.yanqu.road.logic.model.kowloon.sanxiao.KowloonBlockEffect;
import com.yanqu.road.logic.model.kowloon.sanxiao.KowloonRoundResult;
import com.yanqu.road.logic.model.kowloon.sanxiao.KowloonShowResult;
import com.yanqu.road.pb.kowloon.KowloonProto;

import java.util.ArrayList;
import java.util.List;

/**
 * 九龙城寨
 */
public class KowloonPb {

    public static KowloonProto.KowloonUserDataTemp.Builder parseKowloonUserDataTemp(KowloonUserData userData) {
        if (userData == null) {
            return null;
        }
        KowloonProto.KowloonUserDataTemp.Builder builder = KowloonProto.KowloonUserDataTemp.newBuilder();
        builder.setChapterId(userData.getChapterId());
        builder.setProsperousValue(userData.getProsperousValue());
        builder.setMoraleLevel(userData.getMoraleLevel());
        builder.setMoraleBroken(userData.isMoraleBroken());
        builder.setSpeedTimes(userData.getSpeedTimes());
        builder.setIncomeReward(userData.getIncomeReward());
        builder.setLastSettlementRewardTime(userData.getLastSettlementRewardTime());
        builder.setLastGetRewardTime(userData.getLastGetRewardTime());
        if (userData.getGameData() != null) {
            builder.setGameData(parseKowloonGameDataMsg(userData.getGameData()));
        }
        builder.setGuideBit(userData.getGuideBit());
        builder.setChallengeTimes(userData.getChallengeTimes());
        return builder;
    }

    public static KowloonProto.KowloonBuildingDataTemp.Builder parseKowloonBuildingDataTemp(KowloonBuildingData buildingData) {
        if (buildingData == null) {
            return null;
        }
        KowloonProto.KowloonBuildingDataTemp.Builder builder = KowloonProto.KowloonBuildingDataTemp.newBuilder();
        builder.setBuildingId(buildingData.getBuildingId());
        builder.setLevel(buildingData.getLevel());
        return builder;
    }

    public static KowloonProto.KowloonHeroDataTemp.Builder parseKowloonHeroDataTemp(KowloonHeroData heroData) {
        if (heroData == null) {
            return null;
        }
        KowloonProto.KowloonHeroDataTemp.Builder builder = KowloonProto.KowloonHeroDataTemp.newBuilder();
        builder.setHeroId(heroData.getHeroId());
        builder.setLevel(heroData.getLevel());
        builder.setAttribute(parseKowloonHeroProperty(heroData.getAttributeData()));
        builder.setIndex(heroData.getHeroIdx());
        return builder;
    }

    public static KowloonProto.KowloonGameDataMsg.Builder parseKowloonGameDataMsg(KowloonGameData gameData) {
        return parseKowloonGameDataMsg(gameData, null, null);
    }

    public static KowloonProto.KowloonGameDataMsg.Builder parseKowloonGameDataMsg(KowloonGameData gameData, KowloonBattleData battleData, KowloonProto.KowloonResultMsg.Builder resultBuilder) {
        KowloonProto.KowloonGameDataMsg.Builder builder = KowloonProto.KowloonGameDataMsg.newBuilder();
        builder.setChapterId(gameData.getChapterId());
        builder.setSectionId(gameData.getSectionId());
        builder.setUseStep(gameData.getUseStep());
        builder.setLeftStep(gameData.getLeftStep());
        builder.setHp(gameData.getHp());
        builder.setHpMax(gameData.getMaxHp());
        builder.setBoard(parseGameBordMsg(gameData.getBlockList()));
        builder.addAllChooseSkill(gameData.getChooseSkill());
        builder.addAllBossCol(gameData.getBossCol());
        builder.setShowNo(gameData.getShowNo());
        if (battleData != null) {
            KowloonProto.KowloonBattleData.Builder battleDataTemp = parseKowloonBattleData(battleData, resultBuilder);
            builder.setBattleData(battleDataTemp);
        }
        for (KowloonChooseHeroData heroData : gameData.getChooseHeroList()) {
            builder.addHeroIds(heroData.getHeroId());
        }
        return builder;
    }

    public static KowloonProto.KowloonBattleData.Builder parseKowloonBattleData(KowloonBattleData battleData, KowloonProto.KowloonResultMsg.Builder resultBuilder) {
        if (battleData == null) {
            return null;
        }
        KowloonProto.KowloonBattleData.Builder builder = KowloonProto.KowloonBattleData.newBuilder();
        builder.setRandomSeed(battleData.getRandomSeed());
        for (KowloonBattleHeroData battleHeroData : battleData.getHeroDataList()) {
            builder.addHeroList(parseKowloonBattleHeroData(battleHeroData));
        }
        for (KowloonBattleEnemyData enemyData : battleData.getEnemyDataList()) {
            builder.addEnemyList(parseKowloonEnemyData(enemyData));
        }
        if (resultBuilder != null) {
            builder.setResult(resultBuilder);
        }
        return builder;
    }

    public static KowloonProto.KowloonHeroData.Builder parseKowloonBattleHeroData(KowloonBattleHeroData battleHeroData) {
        KowloonProto.KowloonHeroData.Builder builder = KowloonProto.KowloonHeroData.newBuilder();
        builder.setType(battleHeroData.getHeroId());
        builder.setLevel(battleHeroData.getMergeLevel());
        KowloonProto.KowloonCoord.Builder posTemp = KowloonProto.KowloonCoord.newBuilder();
        posTemp.setCol(battleHeroData.getPos().getCol());
        posTemp.setRow(battleHeroData.getPos().getRow());
        builder.setPos(posTemp);
        builder.setProperty(parseKowloonHeroProperty(battleHeroData.getProperty()));
        return builder;
    }

    public static KowloonProto.KowloonEnemyData.Builder parseKowloonEnemyData(KowloonBattleEnemyData enemyData) {
        KowloonProto.KowloonEnemyData.Builder builder = KowloonProto.KowloonEnemyData.newBuilder();
        builder.setType(enemyData.getEnemyId());
        builder.setBornTime(enemyData.getBornTime());
        KowloonNpcAttributeData attributeData = enemyData.getAttributeData();
        builder.setHp(attributeData.getHp());
        builder.setDamage(enemyData.getDamage());
        for (Vec2 vec2 : enemyData.getPath()) {
            builder.addPath(parseKowloonPos(vec2));
        }
        builder.setHitRange(parseKowloonRange(attributeData.getHitRange()));
        builder.setSpeed(attributeData.getMoveSpeed());
        builder.setScale(enemyData.getResourceSize());
        if (enemyData.getBuffCd() != null) {
            builder.addAllBuffCD(enemyData.getBuffCd());
        }
        return builder;
    }

    public static KowloonProto.KowloonPos.Builder parseKowloonPos(Vec2 vec2) {
        KowloonProto.KowloonPos.Builder builder = KowloonProto.KowloonPos.newBuilder();
        builder.setX((int) vec2.getX());
        builder.setY((int) vec2.getY());
        builder.setMoveType(vec2.getMoveType());
        return builder;
    }

    private static KowloonProto.KowloonHeroProperty.Builder parseKowloonHeroProperty(KowloonNpcAttributeData attributeData) {
        KowloonProto.KowloonHeroProperty.Builder builder = KowloonProto.KowloonHeroProperty.newBuilder();
        builder.setAtk(attributeData.getAttack());
        builder.setAtkSpeed(attributeData.getAtkSpeed());
        builder.setAttackRange(parseKowloonRange(attributeData.getAttackRange()));
        builder.setHitRange(parseKowloonRange(attributeData.getHitRange()));
        builder.setMoveSpeed(attributeData.getMoveSpeed());
        builder.setBullet(attributeData.getBullet());
        builder.setAttackNum(attributeData.getAttackNum());
        builder.setSlowBuff(parseKowloonBuffParam(attributeData.getSlowBuff()));
        builder.setFreezeBuff(parseKowloonBuffParam(attributeData.getFreezeBuff()));
        builder.setBleedingBuff(parseKowloonBuffParam(attributeData.getBleedingBuff()));
        builder.setVertigoBuff(parseKowloonBuffParam(attributeData.getVertigoBuff()));
        builder.setEjectionRange(attributeData.getEjectionRange());
        builder.setWeaponRadius(attributeData.getWeaponRadius());
        builder.setBeatBackBuff(parseKowloonBuffParam(attributeData.getBeatBackBuff()));
        builder.setAtkBuff(parseKowloonBuffParam(attributeData.getAtkBuff()));
        builder.setThunderBuff(parseKowloonBuffParam(attributeData.getThunderBuff()));
        return builder;
    }

    private static KowloonProto.KowloonBuffParam.Builder parseKowloonBuffParam(KowloonBuffParam buffParam) {
        KowloonProto.KowloonBuffParam.Builder builder = KowloonProto.KowloonBuffParam.newBuilder();
        builder.setProbability(buffParam.getProbability());
        builder.setDuration(buffParam.getDuration());
        builder.setBuffMaxCount(buffParam.getBuffMaxCount());
        builder.addAllParam(buffParam.getParam());
        builder.addAllParam2(buffParam.getParam2());
        return builder;
    }

    private static KowloonProto.KowloonRange.Builder parseKowloonRange(KowloonRange range) {
        KowloonProto.KowloonRange.Builder builder = KowloonProto.KowloonRange.newBuilder();
        builder.setHeight(range.getHeight());
        builder.setWidth(range.getWidth());
        return builder;
    }

    public static KowloonProto.KowloonBoardMsg.Builder parseGameBordMsg(List<List<KowloonBlock>> listList) {
        KowloonProto.KowloonBoardMsg.Builder builder = KowloonProto.KowloonBoardMsg.newBuilder();
        for (List<KowloonBlock> yBlocks : listList) {
            for (KowloonBlock xBlock : yBlocks) {
                builder.addBlockList(parseBlockMsg(xBlock));
            }
        }
        return builder;
    }

    public static KowloonProto.KowloonBlockMsg.Builder parseBlockMsg(KowloonBlock block) {
        KowloonProto.KowloonBlockMsg.Builder builder = KowloonProto.KowloonBlockMsg.newBuilder();
        builder.setX(block.getX());
        builder.setY(block.getY());
        builder.setType(block.getType());
        builder.setLevel(block.getLevel());
        builder.setStepAdd(block.getStepAdd());
        builder.setHightlight(block.getHightlight());
        builder.setStatus(block.getStatus());
        return builder;
    }

    public static List<KowloonProto.KowloonShowMsg> parseShowListMsg(List<KowloonShowResult> showResultList){
        List<KowloonProto.KowloonShowMsg> showMsgList = new ArrayList<>();
        for (KowloonShowResult showResult : showResultList) {
            showMsgList.add(parseShowMsg(showResult).build());
        }
        return showMsgList;
    }

    public static KowloonProto.KowloonShowMsg.Builder parseShowMsg(KowloonShowResult showResult) {
        KowloonProto.KowloonShowMsg.Builder builder = KowloonProto.KowloonShowMsg.newBuilder();
        for (KowloonBlockEffect effect : showResult.getSkillEffectList()) {
            builder.addEffects(parseEffectMsg(effect));
        }
        for (KowloonRoundResult roundResult : showResult.getRoundResults()) {
            builder.addRoundResult(parseRoundResultMsg(roundResult));
        }

        return builder;
    }

    public static KowloonProto.KowloonEffectMsg.Builder parseEffectMsg(KowloonBlockEffect effect) {
        KowloonProto.KowloonEffectMsg.Builder builder = KowloonProto.KowloonEffectMsg.newBuilder();
        for (KowloonBlock block : effect.getBlockList()) {
            builder.addBlockList(parseBlockMsg(block));
        }

        builder.setEffectType(effect.getEffectType());
        builder.addAllParam(effect.getValueList());

        return builder;
    }

    public static KowloonProto.KowloonRoundResultMsg.Builder parseRoundResultMsg(KowloonRoundResult roundResult) {
        KowloonProto.KowloonRoundResultMsg.Builder builder = KowloonProto.KowloonRoundResultMsg.newBuilder();
        builder.setBoard(parseGameBordMsg(roundResult.getListList()));
        for (KowloonBlock block : roundResult.getElimateBlockList()) {
            builder.addBlockList(parseBlockMsg(block));
        }
        for (KowloonBlock block : roundResult.getCreateBlock()) {
            builder.addCreateBlock(parseBlockMsg(block));
        }

        return builder;
    }

}
